from abc import ABC
from rest_framework import serializers
from account.models import User, Avatar
from article.serializers import ClassifySerialzier, TagSerialzier


class LoginSeralizer(serializers.Serializer):
    username = serializers.CharField(min_length=3, max_length=25, required=True)
    password = serializers.CharField(min_length=6, max_length=20, required=True)
    verify_code = serializers.CharField(max_length=4, min_length=4, required=True)
    uuid = serializers.CharField(required=True)


# class RegisterSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = User
#         exclude = ['id', 'last_login', 'create_date', 'last_change_date']


class RegisterSerializer(serializers.Serializer):
    username = serializers.CharField(max_length=12, min_length=3, write_only=True)
    password = serializers.CharField(max_length=128, min_length=3, write_only=True)
    phone_number = serializers.RegexField(max_length=11, min_length=11, required=False, write_only=True, default=None,
                                          regex=r'^1[3-9]\d{9}$',
                                          error_messages={'invalid': '不正确的手机号码'})
    email = serializers.EmailField(required=False, write_only=True, default=None)
    is_admin = serializers.BooleanField(required=False, default=False)


class ChangePasswordSerializer(serializers.Serializer):
    old_pass = serializers.CharField(required=True)
    new_pass = serializers.CharField(required=True)


# class UserInfoSerializer(serializers.Serializer):
#     class AvatarSerializer(serializers.ModelSerializer):
#         class Meta:
#             model = Avatar
#             fields = ['id', 'avatar_url']
#     user_name = serializers.CharField(required=False, write_only=True, default=None)
#     username = serializers.CharField(max_length=12, min_length=3, read_only=True)
#     last_login = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M:%S')
#     email = serializers.EmailField(read_only=True, default=None)
#     phone_number = serializers.CharField(read_only=True)
#     is_admin = serializers.BooleanField(read_only=True, default=False)
#     # avatar = serializers.SlugRelatedField(allow_null=True, slug_field='id', read_only=True)
#     avatar = AvatarSerializer(read_only=True)

class UserInfoSerializer(serializers.Serializer):
    user_name = serializers.CharField(required=False, write_only=True, default=None)
    username = serializers.CharField(max_length=12, min_length=3, read_only=True)
    last_login = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M:%S')
    email = serializers.EmailField(read_only=True, default=None)
    phone_number = serializers.CharField(read_only=True)
    is_admin = serializers.BooleanField(read_only=True, default=False)
    avatar_url = serializers.SlugRelatedField(allow_null=True, slug_field='avatar_url', read_only=True)


class UpdateUserInfoSeralizer(serializers.Serializer):
    username = serializers.CharField(max_length=12, min_length=3, write_only=True, required=False)
    email = serializers.EmailField(write_only=True, required=False)
    phone_number = serializers.RegexField(max_length=11, min_length=11, required=False, write_only=True,
                                          regex=r'^1[3-9]\d{9}$',
                                          error_messages={'invalid': '不正确的手机号码'})


class UploadAvatarSeralizer(serializers.Serializer):
    name = serializers.CharField(required=False)
    file = serializers.FileField()


class ListClassifySerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    classify_name = serializers.CharField(read_only=True)


class ListTagSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    tag_name_contains = serializers.CharField(write_only=True, required=False, default=None)
    tag_name = serializers.CharField(read_only=True)


class AttentionUserSerializer(serializers.Serializer):
    username = serializers.CharField()


class CancelAttentionSerializer(serializers.Serializer):
    username = serializers.CharField()


class CancelCollectArticleSerializer(serializers.Serializer):
    ids = serializers.ListField(required=True, child=serializers.IntegerField())


class ListCollectArticleSeralizer(serializers.Serializer):
    username = serializers.CharField(write_only=True, required=False)
    id = serializers.IntegerField(read_only=True)
    author = serializers.CharField(source='author.username', read_only=True)
    title = serializers.CharField(read_only=True)
    classifies = ClassifySerialzier(many=True, read_only=True)
    tags = TagSerialzier(many=True, read_only=True)
    publish_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M:%S')
    modify_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M:%S')
    # content = serializers.CharField(read_only=True)
    text_content = serializers.CharField(read_only=True)


class CheckAttentionInfoSerializer(serializers.Serializer):
    username = serializers.CharField()


class ListAttentionUserSerializer(serializers.Serializer):
    user_name = serializers.CharField(required=False, write_only=True, default=None)
    id = serializers.IntegerField(read_only=True)
    username = serializers.CharField(max_length=12, min_length=3, read_only=True)
    avatar_url = serializers.SlugRelatedField(read_only=True, slug_field='avatar_url', allow_null=True)


class DeleteTagsSerializer(serializers.Serializer):
    ids = serializers.ListField(required=True, child=serializers.IntegerField())


class FansCountSerialzer(serializers.Serializer):
    username = serializers.CharField()


class ArticleCountSerialzer(serializers.Serializer):
    user_id = serializers.IntegerField()